/*
 * @(#)PermutationsIterator.java 1.0 15/11/13
 *
 */

package dominio;

import java.util.ArrayList;
import java.util.Collections;

/**
 * Created by Rafael Simon Garcia on 11/15/13.
 */
public class PermutationsIterator {
    private int iCounter, jCounter; //counters for the coordinates vectors
    private int length; //length of each coordinate vector
    private ArrayList<Integer> iValues, jValues; //coordinates vectors

    //constructor receiving the length for both vectors
    public PermutationsIterator(int l){
        this.length = l;
        this.iValues = new ArrayList<>(this.length);
        this.jValues = new ArrayList<>(this.length);
        this.Reset();
    }

    //reasets the iterator
    public void Reset(){
        //clear
        this.iValues.clear();
        this.jValues.clear();
        this.iCounter = 0;
        this.jCounter = 0;

        //reset initial values
        for(int k = 0; k< this.length; k++){
            this.iValues.add(k);
            this.jValues.add(k);
        }

        //reshuffle
        Collections.shuffle(iValues);
        Collections.shuffle(jValues);
    }

    //returns the next pair value generated from the iterator
    public ValuesPair NextValue(){
        ValuesPair pair = new ValuesPair(this.iValues.get(iCounter), this.jValues.get(jCounter));
        UpdateCounters();
        return pair;
    }
    //checks if there are more values available on the vectors
    public boolean HasNextValue(){
        return (iCounter < this.length);
    }
    //updates the coutners of the iterator
    private void UpdateCounters(){
        this.jCounter+= 1;
        if(jCounter >= this.length){
            jCounter = 0;
            iCounter += 1;
        }
    }
}
